package common.fengkai.filepost.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.net.HttpURLConnection;
import java.util.LinkedHashMap;
import java.util.Map;

@Slf4j
public class TestEsealForHttpClient {

    public static void main (String args[]) throws Exception {
        byte[] bytes = Base64util.decoderFileTobytes("C:\\Users\\carry\\Desktop\\项目资料\\非车简易测试文件\\66801060620200000006.pdf");
        String s = Base64util.encodeBase64File("H:\\微信下载文件\\WeChat Files\\wxid_7913629136512\\FileStorage\\File\\2021-05\\保单测试.pdf");
        //soap服务地址
        // String url = "http://10.0.100.57:7001/iSignatureWebPDF/services/PdfActionService";
        String url = "http://10.29.31.58:8002/eseal/iSignatureWebPDF/services/PdfIdActionService";
        // String url = "http://30.79.111.86/eseal/iSignatureWebPDF/services/PdfActionService";
        TestEsealForHttpClient testEsealForHttpClient = new TestEsealForHttpClient();
        Map<String, Object> map = new LinkedHashMap<String,Object>();
        map.put("file",s);
        map.put("json",TestEsealForHttpClient.getString());
        String xmlString = testEsealForHttpClient.makeXml(map);
        System.out.println(xmlString);
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpRequest = new HttpPost(url);
        httpRequest.setHeader("Content-Type", "text/xml");
        httpRequest.setEntity(new StringEntity(xmlString,"UTF-8"));

        HttpResponse response = httpClient.execute(httpRequest);

        HttpEntity entity = response.getEntity();//
        if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
            String result = EntityUtils.toString(entity);

            System.out.println(result);
        }


    }

    /**
     * 生成请求xml数据
     *
     * @param
     * @param todoInfo   数据 (key为wsdl文件中参数的name值注意大小写和顺序都要保持一致,value为实际值)
     * @return
     */
    private String makeXml (Map<String, Object> todoInfo) {


        StringBuffer sb = new StringBuffer();
        sb.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.sealCenter.paic.com/\">\n" +
               "  <soapenv:Header/>\n"+
                "  <soapenv:Body>\n" +
                "    <" + "web:signedPDF" + ">\n");
        sb.append("<pdf>");
        sb.append(todoInfo.get("file"));
        sb.append("</pdf>");
        sb.append("<json>");
        sb.append(todoInfo.get("json").toString().replaceAll("&", "&amp;"));
        sb.append("</json>");
        sb.append("    </" + "web:signedPDF" + ">\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>");

        return sb.toString();
    }

    public static String getString(){
        return "{\n" +
                "\t\"signature\": [{\n" +
                "\t\t\"provider\": \"swxa\",\n" +
                "\t\t\"seal\": {\n" +
                "\t\t\t\"sealName\": \"太平财产保险有限公司公章\",\n" +
                "\t\t\t\"keysn\": \"sichuan\"\n" +
                "\t\t},\n" +
                "\t\t\"position\": {\n" +
                "\t\t\t\"page\": 0,\n" +
                "\t\t\t\"text\": \"投保人名称叶澜\"\n" +
                "\t\t},\n" +
                "\t\t\"key\": 1\n" +
                "\t}]\n" +
                "}\n";
    }

}
